﻿/*
    Copyright Multi-Soft co.2009 
*/
using System;
using System.Collections.Generic;
using System.Data;
using System.Collections;
using Microsoft.Win32;

namespace Rmlife.Common
{
    /// <summary>
    /// 注册表工具类
    /// </summary>
    public class RegistryUtils
    {
        /// <summary>
        /// 读取注册表键值
        /// </summary>
        /// <param name="RegPath"></param>
        /// <param name="RegKey"></param>
        /// <returns></returns>
        public static string ReadString(string RegPath, string RegKey)
        {
            return ReadString(RegPath, RegKey, null);
        }
        /// <summary>
        /// 读取注册表键值
        /// </summary>
        /// <param name="RegPath"></param>
        /// <param name="RegKey"></param>
        /// <param name="DefaultValue"></param>
        /// <returns></returns>
        public static string ReadString(string RegPath, string RegKey, string DefaultValue)
        {
            string sRet = string.Empty;
            try
            {
                RegistryKey objKey = Registry.LocalMachine;
                RegistryKey subReg = objKey.OpenSubKey(RegPath);
                sRet = subReg.GetValue(RegKey, DefaultValue, RegistryValueOptions.DoNotExpandEnvironmentNames).ToString();
                subReg.Close();
                objKey.Close();
            }
            catch
            {
                sRet = null;
            }

            if (string.IsNullOrEmpty(sRet))
            {
                if (!string.IsNullOrEmpty(DefaultValue))
                    return DefaultValue;
                else
                    return null;
            }
            else
                return sRet;
        }

        /// <summary>
        /// 读取注册表键值组
        /// </summary>
        /// <param name="RegPath"></param>
        /// <param name="RegKey"></param>
        /// <param name="KeyPrefix"></param>
        /// <returns></returns>
        public static string[] ReadStringList(string RegPath)
        {
            return ReadStringList(RegPath, string.Empty);
        }
        /// <summary>
        /// 读取注册表键值组
        /// </summary>
        /// <param name="RegPath"></param>
        /// <param name="RegKey"></param>
        /// <param name="KeyPrefix"></param>
        /// <returns></returns>
        public static string[] ReadStringList(string RegPath, string KeyPrefix)
        {
            try
            {
                RegistryKey objKey = Registry.LocalMachine;
                RegistryKey subReg = objKey.OpenSubKey(RegPath);
                Hashtable ht = new Hashtable();
                foreach (string line in subReg.GetValueNames())
                {
                    if (!string.IsNullOrEmpty(KeyPrefix))
                    {
                        if (line.StartsWith(KeyPrefix))
                        {
                            ht[line] = subReg.GetValue(line).ToString();
                        }
                    }
                    else
                    {
                        ht[line] = subReg.GetValue(line).ToString();
                    }
                }
                string[] result = new string[ht.Count];

                ht.Values.CopyTo(result, 0);
                subReg.Close();
                objKey.Close();

                return result;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 写注册表:需要IIS中应用程序池设置为本地用户
        /// </summary>
        /// <param name="RegPath"></param>
        /// <param name="RegKey"></param>
        /// <param name="Value"></param>
        /// <returns></returns>
        public static bool WriteString(string RegPath, string RegKey, object Value)
        {
            return WriteString(RegPath, RegKey, Value, RegistryValueKind.String);
        }
        /// <summary>
        /// 写注册表:需要IIS中应用程序池设置为本地用户
        /// </summary>
        /// <param name="RegPath"></param>
        /// <param name="RegKey"></param>
        /// <param name="Value"></param>
        /// <param name="valueType"></param>
        /// <returns></returns>
        public static bool WriteString(string RegPath, string RegKey, object Value, RegistryValueKind valueType)
        {
            bool ret = false;
            try
            {
                RegistryKey objKey = Registry.LocalMachine;
                RegistryKey subReg = objKey.OpenSubKey(RegPath, true);

                subReg.SetValue(RegKey, Value, valueType);

                ret = true;

                subReg.Close();
                objKey.Close();
            }
            catch
            {
            }
            return ret;
        }
        /// <summary>
        /// 检测注册表路径是否存在
        /// </summary>
        /// <param name="RegPath"></param>
        /// <returns></returns>
        public static bool CheckPath(string RegPath)
        {
            try
            {
                bool ret = false;
                RegistryKey objKey = Registry.LocalMachine;
                RegistryKey subReg = objKey.OpenSubKey(RegPath);

                if (subReg != null)
                {
                    ret = true;
                    subReg.Close();
                }
                objKey.Close();

                return ret;
            }
            catch
            {
                return false;
            }
        }
    }
}
